import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_eve_plugin/components/health/weight_advice.dart';
import 'package:flutter_eve_plugin/components/health/weight_analysis.dart';
import 'package:flutter_eve_plugin/components/health/weight_head.dart';
import 'package:flutter_eve_plugin/components/health/weight_today_dynamic.dart';
import 'package:flutter_eve_plugin/components/main/app_bar.dart';
import 'package:flutter_eve_plugin/constants/constant.dart';
import 'package:flutter_eve_plugin/model/health/health_log.dart';
import 'package:flutter_eve_plugin/pages/health/actions.dart';
import 'package:flutter_eve_plugin/pages/main/actions.dart';
import 'package:flutter_eve_plugin/plugin/main.dart';
import 'package:flutter_eve_plugin/store/main.dart';
import 'package:flutter_eve_plugin/store_vm/app_vm.dart';
import 'package:flutter_eve_plugin/utils/screen_util.dart';
import 'package:flutter_redux/flutter_redux.dart';

class WeightDetailsPage extends StatefulWidget {
  final String title;

  WeightDetailsPage({Key key, this.title}) : super(key: key);

  @override
  _WeightDetailsPageState createState() => _WeightDetailsPageState();
}

class _WeightDetailsPageState extends State<WeightDetailsPage> {
  String logType;
  HealthLogModel logDataModel;

  void loadLogData(int lastItemTime, {Function() callback}) {
    logType = 'weight';
    var req = HealthLogReq(lastItemTime, logType, 20);
    PluginEve.sendMessage(PluginConstant.httpHealthLog,
            param: req, httpType: HttpType.POST)
        .then((data) {
      if (callback != null) {
        callback();
      }
      setState(() {
        if (lastItemTime <= 0) {
          logDataModel = data;
        } else {
          HealthLogModel tmp = data;
          logDataModel?.list?.addAll(tmp?.list);
        }
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return StoreConnector<AppState, AppStateViewModel>(
        onInit: (store) {
          PluginEve.sendMessage(PluginConstant.httpHealthBody, httpType: HttpType.POST)
              .then((data) => store.dispatch(healthPropertyBody(store, data)));
          PluginEve.sendMessage(PluginConstant.httpUserInfo, httpType: HttpType.POST)
              .then((data) => store.dispatch(userInfo(store, data)));
          loadLogData(0);
        },
        converter: (store) => AppStateViewModel(
              state: store.state,
            ),
        builder: (context, vm) {
          return Scaffold(
              appBar: buildAppBar(context, title: widget.title),
              body: CustomScrollView(
                  physics: BouncingScrollPhysics(),
                  slivers: <Widget>[
                    SliverToBoxAdapter(
                      child: Container(
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: <Widget>[
                            WeightPageHeadWidget(),
                            SizedBox(
                              height: setRatio(40),
                            ),
                            TodayDynamicWidget(),
                            SizedBox(
                              height: setRatio(40),
                            ),
                            WeightAdviceWidget()
                          ],
                        ),
                        margin: EdgeInsets.only(
                            left: setRatio(15),
                            top: setRatio(20),
                            right: setRatio(15),
                            bottom: setRatio(30)),
                      ),
                    ),
                    SliverToBoxAdapter(
                        child: WeightAnalysisWidget(
                      title: '体重分析',
                      logModel: logDataModel,
                      type: logType,
                      logRefresh: (String type, int lastTime,
                          GlobalKey<RefreshHeaderState> headerKey) {
                        loadLogData(lastTime, callback: () {
                          headerKey.currentState.onRefreshEnd();
                        });
                      },
                      logLoadMore: (String type, int lastTime,
                          GlobalKey<RefreshFooterState> footerKey) {
                        loadLogData(lastTime, callback: () {
                          footerKey.currentState.onLoadEnd();
                        });
                      },
                    ))
                  ]));
        });
  }
}
